package user

import (
	"awesome/model"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

func Getusers(c *gin.Context) {
	fmt.Println("来了")
	//var users []model.User
	var user model.User
	model.DB.Model(&model.User{}).Preload("CreditCard").Preload("Orders").Find(&user, 1)
	//model.DB.Find(&users, []int{1, 2, 3})

	c.JSON(http.StatusOK, gin.H{
		"msg":   "ok",
		"data":  user,
		"error": 0,
	})
}
func Getcategories(c *gin.Context) { //获取所有分类
	var cates []*model.AvatarCategory
	model.DB.Where("parent_id is NULL").Find(&cates)

	for _, cate := range cates {
		var cc []model.AvatarCategory
		model.DB.Where("parent_id = ?", cate.CategoryId).Find(&cc)
		cate.Children = cc
	}

	c.JSON(http.StatusOK, gin.H{
		"msg":   "ok",
		"data":  cates,
		"error": 0,
	})
}
func Getavatar(c *gin.Context) { //获取该分类下所有形象
	var cates []*model.AvatarStoreCategory
	cid := c.PostForm("cid")
	model.DB.Where("category_id = ?", cid).Find(&cates)

	var results []model.AvatarStore
	for _, cate := range cates {
		avatar := model.AvatarStore{}
		model.DB.Where("avatar_id = ?", cate.AvatarId).First(&avatar)
		results = append(results, avatar)
	}

	c.JSON(http.StatusOK, gin.H{
		"msg":   "ok",
		"data":  results,
		"error": 0,
	})
}
